package crmdna.member;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import crmdna.client.Client;
import crmdna.common.contact.ContactProp;
import crmdna.group.Group;
import crmdna.group.Group.GroupProp;
import crmdna.practice.Practice;
import crmdna.practice.Practice.PracticeProp;
import crmdna.program.Program;
import crmdna.program.ProgramProp;

public class MemberProp implements Comparable<MemberProp> {
	public long memberId;
	public ContactProp contact;
	public String name;

	// public Set<String> qsTags = new HashSet<>();
	public Set<Long> groupIds = new HashSet<Long>();
	public Set<Long> programIds = new HashSet<Long>();
	public Set<Long> programTypeIds = new HashSet<Long>();
	public Set<Long> practiceIds = new HashSet<Long>();
	
	public List<UnverifiedProgramProp> unverifiedProgramProps = new ArrayList<>();
	

	//dependents
	public Set<String> practices = new HashSet<>();
	public List<ProgramProp> programProps = new ArrayList<>();
	public Set<String> groups = new HashSet<>();

	@Override
	public int compareTo(MemberProp o) {
		return this.name.compareTo(o.name);
	}
	
	public static void populatePractices(String client, List<MemberProp> memberProps) {
		Client.ensureValid(client);
		
		Set<Long> practiceIds = new HashSet<>();
		
		for (MemberProp prop : memberProps) {
			practiceIds.addAll(prop.practiceIds);
		}
		
		Map<Long, PracticeProp> practiceMap = Practice.get(client, practiceIds);
				
		for (MemberProp prop : memberProps) {
			prop.practices = new HashSet<>();
			for (Long practiceId : prop.practiceIds) {
				if (practiceMap.containsKey(practiceId))
					prop.practices.add(practiceMap.get(practiceId).displayName);
			}
		}				
	}
	
	public static void populateGroups(String client, List<MemberProp> memberProps) {
		Client.ensureValid(client);
		
		Set<Long> groupIds = new HashSet<>();
		
		for (MemberProp prop : memberProps) {
			groupIds.addAll(prop.groupIds);
		}
		
		Map<Long, GroupProp> map = Group.get(client, groupIds);
				
		for (MemberProp prop : memberProps) {
			prop.groups = new HashSet<>();
			for (Long groupId : prop.groupIds) {
				if (map.containsKey(groupId))
					prop.groups.add(map.get(groupId).displayName);
			}
		}				
	}
	
	public static void populateProgramProps(String client, List<MemberProp> memberProps) {
		Client.ensureValid(client);
		
		Set<Long> programIds = new HashSet<>();
		
		for (MemberProp prop : memberProps) {;
			programIds.addAll(prop.programIds);
		}
		
		Map<Long, ProgramProp> programeMap = Program.get(client, programIds);
				
		for (MemberProp prop : memberProps) {
			prop.programProps = new ArrayList<>();
			for (Long id : prop.programIds) {
				if (programeMap.containsKey(id))
					prop.programProps.add(programeMap.get(id));
			}
			Collections.sort(prop.programProps);			
		}				
	}
}